$m(function(args) {
	var that = this;
	var $this = this.element;
	var pageUrl = args.pageUrl;
	var workSpace = args.workSpace;
	var workSpaceId =workSpace?workSpace.id:null;
	/**
	 * 已选列表
	 */
	var grid = $this.find("#tempo-workspace-permission-grid");
	/**
	 * 初始化
	 */
	var init = function(){
		renderGrid();
	}
	/**
	 * 绘制人员列表
	 */
	var renderGrid = function(){
		var rqId = workSpaceId;
		grid.datagrid({
			url : contextPath+"/api/ai/workspace/getWorkSpacePrivisPost",
			datatype:'json',
			type : 'post',
			fit : false,
			rownumbers : true,
			colNames:getColName(),
			colModel:getColModel(),
			rowNum:1000,
			width:450,
			height:140,
			pagination:false,
			multiselect : true,
			postData:{
				id : rqId,
				name:''
			},
			loadComplete:function(data){
				//去除冒泡提示
				$('td[aria-describedby=tempo-workspace-permission-grid_TYPE]').removeAttr('title');
			},
			sortable:false,
			searchBar:false,
			searchBarOptions:{
				placeholder:'请输入人员名称或编码',
				onSearch:function(value){
					grid.datagrid("reloadGrid",{name:value},true);
				}
			},
			toolbar:getToolbar()
		});
	}

	/**
	 * 列名称
	 */
	var getColName = function(){
		var names = ["id","CODE","OBJID",
			TempoUtils.locale("bidesign.workspace_priv_user_name"),
			TempoUtils.locale("bidesign.workspace_priv_user_code"),
			TempoUtils.locale("bidesign.workspace_priv_type")
		];
		return names;
	}

	/**
	 * 列模型
	 */
	var getColModel = function(){
		var arr = [{
			name:'ID',
			id : true,
			width:100,
			hidden:true
		},{
			name:'CODE',
			width:100,
			hidden:true
		},{
			name:'OBJID',
			width:100,
			hidden:true
		},{
			name:'NAME',
			width:100,
			sortable:false
		},{
			name:'USERCODE',
			width:100,
			sortable:false
		},{
			name:'TYPE',
			width:177,
			formatter : function(d) {
				var id=TempoUtils.uuid();
				d==null?d='2':d;
				var result="<input value='1'   style='margin-left:3px;margin-bottom: 2px;vertical-align: middle;' name='type"+id+"' id='type"+id+"1' type='radio' "+(d=='2'?"checked":"")+" /><label for='type"+id+"1' >"+TempoUtils.locale("bidesign.workspace_perm_use")+"</label>"+
					"<input name='type"+id+"' value='2' style='margin-left:17px;margin-bottom: 2px;vertical-align: middle;' type='radio' id='type"+id+"2' "+(d=='1'?"checked":"")+"/><label for='type"+id+"2' >"+TempoUtils.locale("bidesign.workspace_perm_manage")+"</label>";
				return result;
			},
			sortable:false
		}];
		return arr;
	}


	/**
	 * 工具条
	 */
	var getToolbar = function(){
		var toolbar = [{
			id:"add",
			text:TempoUtils.locale("bidesign.workspace_new"),
			click:function(e){
				toolbarEvent["add"]();
			}
		},{
			id:"remove",
			text:TempoUtils.locale("bidesign.remove"),
			click:function(e){
				toolbarEvent["remove"]();
			}
		},{
			id : "useprivilege",
			text : TempoUtils.locale("aidsview.usePrivilege"),
			click : function(e){
				toolbarEvent["useprivilege"]();
			}
		},{
			id : "manageprivilege",
			text : TempoUtils.locale("aidsview.managePrivilege"),
			click : function(e){
				toolbarEvent["manageprivilege"]();
			}
		}];
		return toolbar;
	}



	/**
	 * 工具条事件处理
	 */
	var toolbarEvent = {
		add:function(){
			var data = grid.datagrid('getRowData');
			var codes = [];
			if(data && data.length > 0){
				var ids = [];
				var types=[];
				for (var i = 0; i < data.length; i++) {
					codes.push(workSpaceId==null?data[i].ID:data[i].CODE);
					ids.push(data[i].ID);
					types.push($('input[name='+data[i].TYPE.substr(data[i].TYPE.indexOf("name=")+6,36)+']:first').is(":checked")?"2":"1");
				}
				$.ajax({
					url: contextPath+"/api/ai/workspace/privis/update",
					method: 'POST',
					data: {
						ids : ids.join(','),
						types : types.join(','),
						workSpaceId:workSpaceId
					},
					success: function(data) {
					}
				})
			}
			$("<div/>").userselect({
				isFilterCurrentUser:true,
				sysRoleCode : 'SYS_AI_SJRY',
				showRoleTree:true,
				showWorkgroupTree:true,
				isSessionUserOrg:true,
				isSessionUserRole:true,
				isShowAllText:true,
				sessionUserOrgUrl: '/cloud-system-manage/api/userselect/orgtree',
				listUserByOrgIdUrl: '/cloud-system-manage/api/userselect/userinfo/listbyorg',
				listUserByRoleIdUrl: '/cloud-system-manage/api/userselect/userinfo/listbyrole',
				listUserByWorkgroupIdUrl: '/cloud-system-manage/api/userselect/userinfo/listbyworkgroup',
				codes:codes,
				orgUrlParam:{pid:""},
				saveHandler:function(data){
					saveSelectUser(data);
				}
			});
		},
		remove:function(){
			removeUserPermission();
		},
		useprivilege:function () {
			addprivileges("2");

		},
		manageprivilege:function () {
			addprivileges("1");
		}
	}

	/**
	 * 选择用户进行权限配置,选择时，默认为普通权限
	 */
	var saveSelectUser = function(data){
		// if(data && data.length > 0){
		// 	for (var i = 0; i < data.length; i++) {
		// 		var newRowId = TempoUtils.uuid();
		// 		grid.datagrid("addRowData",newRowId,{
		// 			CODE: data[i].empCode,
		// 			DEPTNAME: data[i].deptName,
		// 			EMAIL: "",
		// 			ID: data[i].userId,
		// 			NAME: data[i].empName,
		// 			USERCODE: data[i].userName
		// 		});
		// 	}
		// }
		if(data && data.length > 0&&workSpaceId){
			var userIds = [];
			for (var i = 0; i < data.length; i++) {
				userIds.push(data[i].userId);
			}
			$.ajax({
				url: contextPath+"/api/ai/workspace/privis/create",
				method: 'POST',
				data: {
					workSpaceId : workSpaceId,
					users : userIds.join(',')
				},
				success: function(data) {
					grid.datagrid("reloadGrid");
				}
			})
		}

	}


	/**
	 * 新增人员回调
	 */
	var addUserCallback = function(){
		grid.datagrid("reloadGrid",{},true);
	}

	/**
	 * 删除用户权限
	 */
	var removeUserPermission = function(){
		var selectRows = grid.datagrid("getSelectRowData");
		if(selectRows==null||selectRows.length==0){
			$("<div>").alert({
				msg : '请选择一条记录'
			});
			return;
		}
		$("<div/>").confirm({
			msg:'确定删除？',
			ok:function(){
				var selectRowIds = TempoUtils.clone(grid.datagrid("getGridParam","selarrrow"));
				if(selectRowIds && selectRowIds.length > 0){
					for (var m = 0; m < selectRowIds.length; m++) {
						var rowid = selectRowIds[m];
						grid.datagrid('delRowData', rowid);
					}
				}
				if(workSpaceId==null){
					return;
				}
				var selectIds = [];
				var userCodes = [];
				for (var i = 0; i < selectRows.length; i++) {
					selectIds.push(selectRows[i].ID);
					userCodes.push(selectRows[i].CODE);
				}
				$.ajax({
					url: contextPath+"/api/ai/workspace/privis/delete",
					data:{
						ids : selectIds.join(','),
						workSpaceId:workSpaceId,
						userCodes:userCodes.join(',')

					},
					type:'post',
					success:function(result){
						grid.datagrid("reloadGrid",{},true);
					}
				});
			}
		});
	}

	/*
	* 批量新增使用/管理权限
	* @pa
	* */
	/**
	 * 批量新增使用/管理权限
	 * @param type 权限类型 int 1是管理权限 2是用户权限
	 */
	var addprivileges = function (type) {
		var _this = this;
		var selectRows = grid.datagrid("getSelectRowData");
		if(selectRows==null||selectRows.length==0){
			$("<div>").alert({
				msg : '请选择一条记录'
			});
			return;
		}
		var ids = [],types=[];
		for (var i = 0; i < selectRows.length; i++) {
			ids.push(selectRows[i].ID);
			types.push(type);
		}
		$.ajax({
			url: contextPath+"/api/ai/workspace/privis/update",
			method: 'POST',
			data: {
				ids : ids.join(','),
				types : types.join(','),
				workSpaceId:workSpaceId
			},
			success: function(data) {
				if (type=='2') {
					for (var i = 0; i < grid.datagrid("getSelectRowData").length; i++) {
						var radioId = $(grid.datagrid("getSelectRowData")[i].TYPE)[0].id;
						$('#'+radioId).prop("checked",true)
					}
				}else if (type=='1') {
					for (var i = 0; i < grid.datagrid("getSelectRowData").length; i++) {
						var radioId = $(grid.datagrid("getSelectRowData")[i].TYPE)[2].id;
						$('#'+radioId).prop("checked",true)
					}
				}
			}
		});
	}

	!function(){
		init();
	}();
});